#include "main.h"
#include<math.h>
#include<stddef.h>
int ListInitiate(LNode **head) {
    *head=(LNode *)malloc(sizeof(LNode));
    if(*head == NULL) {
        return 1;
    }
    (*head)->next = NULL;
    return 0;
}
int ListLength(LNode *head) {
    int length = 0;
    LNode *p = head->next;
    while(p!=NULL) {
        length++;
        p = p->next;
    }
    return length;
}
int ListInsert(LNode *head, int i,DataType x) {
    int j = 0;
    LNode *p = head, *s;
    while(p!=NULL&&j<i) {
        p = p->next;
        j++;
    }
    if(p==NULL||j>i) {
        return 1;
    }
    s = (LNode *)malloc(sizeof(LNode));
    s->data = x;
    s->next = p->next;
    p->next = s;
    return 0;
}
int ListDelete(LNode *head, int i, DataType *x) {
    int j = 0;
    LNode *p = head, *q;
    while(p->next!=NULL&&j<i) {
        p = p->next;
        j++;
    }
    if(p->next==NULL||j<i) {
        return 1;
    }
    q = p->next;
    p->next = q->next;
    *x = q->data;
    free(q);
    return 0;
}
int ListGet(LNode *head, int i, DataType *x) {
    int j = 0;
    LNode *p = head->next;
    while(p!=NULL&&j<i) {
        p = p->next;
        j++;
    }
    if(p==NULL||j>i) {
        return 1;
    }
    *x = p->data;
    return 0;
}
int Destroy(LNode **head) {
    LNode *p = *head, *q;
    while(p!=NULL) {
        q = p;
        p = p->next;
    }
    *head = NULL;
    return 0;
}
void main(void) {
    LNode *head;
    int i, x;
    ListInitiate(&head);
    for(i = 0; i<10;i++) {
        if(ListInsert(head,i,i+1)==0) {
            printf("Error \n");
            return;
        }
    }
    if(ListDelete(head ,4,&x)==0) {
        printf("Error \n");
        return;
    }
    for(i=0;i<ListLength(head);i++) {
        if(ListGet(head,i,&x)==0) {
            printf("%d",x);
        }
        else {
            printf("Error \n");
        }
    }
    Destroy(&head);
}